"""
This is based on the Twisted web.tac example, modified for running under
Apache/mod_python.

To run this, you will need to make sure that mod_python is installed and
enabled for your Apache instance and that Apache has the following
configuration in either the main .conf or an appropriate virtual host .conf:

Alias /data/ "/home/oubiwann/lab/pymx/sandbox/oubiwann/"
<Directory "/home/oubiwann/lab/pymx/sandbox/oubiwann/">
    SetHandler mod_python
    PythonHandler mod_python.publisher
    DirectoryIndex index.py
    Options Indexes
    Order allow,deny
    Allow from all
</Directory>

Of course, you'll need to make sure the directories match yours...

After restarting Apache, you can visit this URL:
  http://localhost/data/
where you will see a list of links. Clickig each one will take you to a simple
report on the usage of the given system or python resource.

"""
import resources

def pid(req):
    data = resources.getPID()
    return ('pid', data)

def msgs(req):
    sent = resources.getRUsageSelf().msgsSent
    rec = resources.getRUsageSelf().msgsReceived
    data = (sent, rec)
    return ('msgs', data)

def reclaims(req):
    data = resources.getRUsageSelf().pageReclaims
    return ('reclaims', data)

def swaps(req):
    data = resources.getRUsageSelf().swaps
    return ('swaps', data)

def switches(req):
    vol = resources.getRUsageSelf().volCtxSwitches
    invol = resources.getRUsageSelf().involCtxSwitches
    data = (vol, invol)
    return ('switches', data)

def times(req):
    user = resources.getTimes().user.asSeconds
    system = resources.getTimes().system.asSeconds
    data = (user, system)
    return ('times', data)

def ressize(req):
    data = str(resources.getStat().resSize.mb) + ' MB'
    return ('ressize', data)

def virtsize(req):
    data = str(resources.getStat().virtSize.mb) + ' MB'
    return ('virtsize', data)

def data(req):
    payload = (
        '<html><ul>' +
        '<li><a href="/data/pid">pid</a></li>' +
        '<li><a href="/data/msgs">msgs</a></li>' +
        '<li><a href="/data/reclaims">reclaims</a></li>' +
        '<li><a href="/data/swaps">swaps</a></li>' +
        '<li><a href="/data/switches">switches</a></li>' +
        '<li><a href="/data/times">times</a></li>' +
        '<li><a href="/data/ressize">ressize</a></li>' +
        '<li><a href="/data/virtsize">virtsize</a></li>' +
        '</ul><html>')
    req.content_type = 'text/html'
    req.write(payload)

def index(req):
    return data(req)

